<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Crypt;
include_once 'storage/code/Code.class.php';

class LoginController extends CommonController
{
    // 登录模块
    public function login (Request $request) {
    	$code = new \Code;
    	if ($request->isMethod('POST')) {
                // 获取传入的所有数据
    			$data = $request->all();
                // 获取验证码
    			$_code = $code->get();
    			if ($_code !== strtoupper($data['code'])) {
                    session(['id'=>null]);
    				return back()->with('msg','验证码错误！');
    			} else {
                    // 获取数据库管理员信息（只有一条数据）
    				$admin = DB::table('user')->first();
                    if($data['username'] === $admin->user_name && 
                        $data['password'] == Crypt::decrypt($admin->user_pass)){
                        // 保存session
                        $request->session()->put('username',$data['username']);
                        $request->session()->put('id',$admin->user_id);
                        // 登录成功跳转
                        return redirect('admin/index');
                    } else {

                        return back()->with('msg','用户名或密码错误');
                    }
    			}
    			
    	}
    	return view('admin.login');
    }

    //生成验证码
    public function setCode(){
    	$code = new \Code;
    	$_code = $code->make();
    }
}
